import Wx, {wx} from "./Wx";
import Common from "../utils/Common";

class FeedbackButton {
    private button: any = null;
    private type: string = "text";
    private text: string = "意见反馈";
    private image: string = "";
    private style: any = {};
    private defStyle: any = {
        left: 10,
        top: 100,
    };
    private alignLeft: boolean = true;

    private readonly textStyle = {
        width: 100,
        height: 40,
        lineHeight: 40,
        backgroundColor: '#ff0000',
        color: '#ffffff',
        textAlign: 'center',
        fontSize: 16,
        borderRadius: 4
    }

    private readonly imageStyle = {width: 60, height: 60, backgroundColor: "#ff0000"}

    public setType(type: "text" | "image") {
        this.type = type;
    }

    public setText(text: string) {
        this.text = text;
    }

    public setImage(image: string) {
        this.image = image;
    }

    public setStyle(style: any) {
        this.style = style;
    }

    /**
     * 如果是右对齐的话, left 值将变成 right 值.
     * @param alignLeft
     */
    public setAlignLeft(alignLeft: boolean) {
        this.alignLeft = alignLeft;
    }

    public async show() {
        if (!this.button) {
            this.createButton();
        }
        if (this.button) {
            this.button.show();
        }
    }

    public hide() {
        if (this.button) {
            this.button.hide();
        }
    }

    private createButton() {
        if (!Wx.isWeChatEnv()) {
            return;
        }
        if (!wx.createFeedbackButton) {
            cc.log("版本不支持意见反馈.")
            return;
        }
        if (this.type === "text") {
            this.style = Common.assignM(this.defStyle, this.textStyle, this.style);
        } else {
            this.style = Common.assignM(this.defStyle, this.imageStyle, this.style);
        }
        cc.log("意见反馈按钮样式:", this.style);

        this.button = wx.createFeedbackButton({
            type: this.type || 'text',
            text: this.text || "意见反馈",
            image: this.image || "",
            style: this.style || {}
        })
        let sysInfo = Wx.getSystemInfoSync()
        if (!this.alignLeft) {
            this.button.style.left = sysInfo.windowWidth - this.button.style.width - this.button.style.left;
        }
    }
}

export const feedbackButton: FeedbackButton = new FeedbackButton();